package cn.itcast.bos.service.base.impl;

import java.util.List;
import java.util.Set;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.dom4j.CDATA;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.itcast.bos.dao.base.CourierRepository;
import cn.itcast.bos.domain.base.Courier;
import cn.itcast.bos.service.base.CourierService;
import oracle.net.aso.p;
@Service
@Transactional
public class CourierServiceImpl implements CourierService {
	
	
	@Autowired
	private CourierRepository dao;
	
	

	@Override
	@RequiresPermissions("courier_add")
	public void save(Courier courier) {
		dao.save(courier);
		
	}



	@Override
	public Page<Courier> pageQuery(Specification<Courier> specification,Pageable pageable) {
		Page<Courier> page = dao.findAll(specification,pageable);
		return page;
	}



	@Override
	public void updateDelTag(String[] idArray) {
		if(StringUtils.isNoneBlank(idArray)){
			for (String idStr : idArray) {
				dao.updateDelTag(Integer.parseInt(idStr));
			}	
		}
		
	}



	@Override
	public List<Courier> findnoassociation() {
		Specification<Courier> specification = new Specification<Courier>() {

			@Override
			public Predicate toPredicate(Root<Courier> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				Predicate p = cb.isEmpty(root.get("fixedAreas").as(Set.class));
				return p;
			}
			
		};
		return dao.findAll(specification);
	}

}
